package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USERID;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.AuditrealBean;
import com.luoxiang.socialandroid.bean.AuditrealData;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.RealAuditBean;
import com.luoxiang.socialandroid.bean.UserInfo1Bean;
import com.luoxiang.socialandroid.dialog.RealPersionDialog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.luoxiang.socialandroid.utils.NetUtils2;
import com.luoxiang.socialandroid.view.ScrollChangeLisWebView;
import com.lxj.xpopup.XPopup;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class AuditActivity extends BaseActivity {
    private RelativeLayout r_zrrz, r_smrz;
    private ImageView icon_back;
    private String token;
    private TextView tv_minerenzheng, bilichitext;
    private int code;
    private String msg;
    private PopupWindow popupWindow;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        r_smrz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RealAuditInfo();
            }
        });
    }

    private void RealAuditInfo() {
        NetUtils2.getInstance().getApiService().getRealAuditInfo(token, EXTRA)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStrop", decryptStr);
                            Gson gson = new Gson();
                            RealAuditBean bean = gson.fromJson(decryptStr, RealAuditBean.class);
                            if (bean.getData().getCode() != -1) {
                                Intent intent = new Intent(AuditActivity.this, AuditStatusActivity.class);
                                intent.putExtra("status", bean.getData().getCode());
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(AuditActivity.this, RealAuditActivity.class);
                                startActivity(intent);
                            }

                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_audit);
        setStatusBarTranslucent();
    }

    private void getUserInfo(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().getUserInfo(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr22", decryptStr);
                        Gson gson = new Gson();
                        UserInfo1Bean bean = gson.fromJson(decryptStr, UserInfo1Bean.class);
                        if (bean == null || bean.getData() == null || bean.getData().getUserInfo() == null)
                            return;
                        if (bean.getData().getUserInfo().getHost() == 1) {
                            //表示是主播
                            r_zrrz.setVisibility(View.GONE);
                            r_smrz.setVisibility(View.VISIBLE);
                        } else {
                            r_zrrz.setVisibility(View.VISIBLE);
                            r_smrz.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getNewAuditInfo(EXTRA, token);
    }

    @Override
    protected void initView() {
        icon_back = findViewById(R.id.icon_back);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        r_zrrz = findViewById(R.id.r_zrrz);
        r_smrz = findViewById(R.id.r_smrz);
        tv_minerenzheng = findViewById(R.id.tv_minerenzheng);
        bilichitext = findViewById(R.id.bilichitext);
        r_zrrz.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                if (code == 2 || code == -1) {
                    popupWindow.showAsDropDown(r_zrrz, 130, 0);
                } else {
                    Intent intent = new Intent(AuditActivity.this, RealresultActivity.class);
                    intent.putExtra("realmsg", msg);
                    startActivity(intent);
                }
            }

            @Override
            protected void onFastClick() {

            }
        });

        View view1 = LayoutInflater.from(AuditActivity.this).inflate(R.layout.dialog_audit, null, false);
        popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false);
        ScrollChangeLisWebView webView = popupWindow.getContentView().findViewById(R.id.webview_zhenren);
        webView.loadUrl("http://mlhtml-test.dhxwlkj.com/realCertification/");
        webView.setOnCustomScrollChangeListener(new ScrollChangeLisWebView.ScrollInterface() {
            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {
                //WebView的总高度  有缩放比例
                float webViewContentHeight = webView.getContentHeight() * webView.getScale();
                //WebView的现高度
                float webViewCurrentHeight = (webView.getHeight() + webView.getScrollY());
                if ((webViewContentHeight - webViewCurrentHeight) < 10) {  //尽量不要用==0，有时候精度问题导致无法完全相等(自己调试即可)
                    popupWindow.getContentView().findViewById(R.id.btn_todinish).setBackgroundResource(R.drawable.shape_btn_finish);
                    TextView viewById = popupWindow.getContentView().findViewById(R.id.btn_todinish);
                    viewById.setText(R.string.mi_yuedutongyi);
                    popupWindow.getContentView().findViewById(R.id.btn_todinish).setOnClickListener(new CustomClickListener() {
                        @Override
                        protected void onSingleClick() {
                            RxPermissions rxPermissions = new RxPermissions(AuditActivity.this);
                            rxPermissions.request(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
                                    .subscribe(new Observer<Boolean>() {
                                        @Override
                                        public void onSubscribe(Disposable d) {
                                        }

                                        @Override
                                        public void onNext(Boolean value) {
                                            popupWindow.dismiss();
                                            if (value) {
                                                startActivity(new Intent(AuditActivity.this, CameraVideoActivity.class));
                                            } else {
                                                Toast.makeText(
                                                        AuditActivity.this,
                                                        getString(R.string.mi_camera_video_quanxian),
                                                        Toast.LENGTH_SHORT
                                                ).show();
                                            }
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                        }

                                        @Override
                                        public void onComplete() {
                                        }
                                    });
                        }

                        @Override
                        protected void onFastClick() {

                        }
                    });
                }
            }
        });
        popupWindow.getContentView().findViewById(R.id.tv_zhenren).setOnClickListener(v -> {
            popupWindow.dismiss();
        });
        popupWindow.getContentView().findViewById(R.id.img_chadiao).setOnClickListener(v -> {
            popupWindow.dismiss();
        });
    }

    private void getNewAuditInfo(String extra, String token) {
        NetUtils.getInstance().getApiService().getNewAuditInfo(extra, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                            LogUtils.i("decryptStrgetNewAuditInfo", decryptStr);
                            Gson gson = new Gson();
                            AuditrealBean auditrealBean = gson.fromJson(decryptStr, AuditrealBean.class);
                            code = auditrealBean.getData().getCode();
                            msg = auditrealBean.getData().getMsg();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
